In [2]:
import numpy as np

Scalars


In [3]:
s = np.array(5)

In [4]:
print s


5

In [5]:
s.shape


Out[5]:
()

In [6]:
x = s + 3

In [7]:
print x


8

Vectors


In [8]:
v = np.array([1,2,3])
print v


[1 2 3]

In [9]:
x = v[1]
print x


2

In [11]:
v[1:]


Out[11]:
array([2, 3])

Matrices


In [12]:
m = np.array([[1,2,3], [4,5,6], [7,8,9]])
print m


[[1 2 3]
 [4 5 6]
 [7 8 9]]

In [13]:
m.shape


Out[13]:
(3, 3)

In [14]:
print m[1][2]


6

Tensors


In [16]:
t = np.array([[[[1],[2]],[[3],[4]],[[5],[6]]],[[[7],[8]],\
    [[9],[10]],[[11],[12]]],[[[13],[14]],[[15],[16]],[[17],[17]]]])
print t


[[[[ 1]
   [ 2]]

  [[ 3]
   [ 4]]

  [[ 5]
   [ 6]]]


 [[[ 7]
   [ 8]]

  [[ 9]
   [10]]

  [[11]
   [12]]]


 [[[13]
   [14]]

  [[15]
   [16]]

  [[17]
   [17]]]]

Changing Shapes


In [17]:
v = np.array([1,2,3,4])
print v


[1 2 3 4]

In [18]:
x = v.reshape(1,4)
print x


[[1 2 3 4]]

In [19]:
x = v.reshape(4,1)
print x


[[1]
 [2]
 [3]
 [4]]

In [20]:
x = v.reshape(2,2)
print x


[[1 2]
 [3 4]]

In [21]:
x = v[None, :]
print x


[[1 2 3 4]]

In [22]:
x = v[:, None]
print x


[[1]
 [2]
 [3]
 [4]]

In [23]:
a = np.arange(6)                         # 1d array
print(a)


[0 1 2 3 4 5]

In [24]:
b = np.arange(12).reshape(4,3)           # 2d array
print(b)


[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

In [25]:
c = np.arange(24).reshape(2,3,4)         # 3d array
print(c)


[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]

In [27]:
d = np.arange(24*5).reshape(2,3,4,5)         # 4d array
print(d)


[[[[  0   1   2   3   4]
   [  5   6   7   8   9]
   [ 10  11  12  13  14]
   [ 15  16  17  18  19]]

  [[ 20  21  22  23  24]
   [ 25  26  27  28  29]
   [ 30  31  32  33  34]
   [ 35  36  37  38  39]]

  [[ 40  41  42  43  44]
   [ 45  46  47  48  49]
   [ 50  51  52  53  54]
   [ 55  56  57  58  59]]]


 [[[ 60  61  62  63  64]
   [ 65  66  67  68  69]
   [ 70  71  72  73  74]
   [ 75  76  77  78  79]]

  [[ 80  81  82  83  84]
   [ 85  86  87  88  89]
   [ 90  91  92  93  94]
   [ 95  96  97  98  99]]

  [[100 101 102 103 104]
   [105 106 107 108 109]
   [110 111 112 113 114]
   [115 116 117 118 119]]]]

In [ ]: